//
//  STTools_NetWrokManger.m
//  GodHorses
//
//  Created by coder on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STTools_NetWrokManger.h"
#define st_resp_codeKey @"code"
#define st_resp_tureCode @"200"
#define st_resp_messageKey @"message"
#define st_down_flodleName @"floderName"
@implementation STTools_NetWrokManger
{
    AFHTTPSessionManager *_manager;
    NSMutableDictionary *_requestTaskPool;
}
static const double kAFTimeoutInterval = 15;
static STTools_NetWrokManger *instance = nil;

- (id)init
{
    if (self = [super init]) {
        _manager = [AFHTTPSessionManager manager];
        _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        _manager.requestSerializer.timeoutInterval = kAFTimeoutInterval;
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    }
    return self;
}
#pragma mark - Public Method
+ (STTools_NetWrokManger *)defaultClient
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}
+ (void)stoneobs_fun_shownNormalRespMsgWithResponse:(id)response{
    NSDictionary * dic = response;
    if ([dic.allKeys containsObject:st_resp_messageKey]) {
        NSString * msg = dic[st_resp_messageKey];
        
        [SVProgressHUD showSuccessWithStatus:msg];
        return;
    }
    [SVProgressHUD showSuccessWithStatus:@"操作成功"];
}
- (void)stoneobs_fun_requestWithPath:(NSString *)url
                 method:(STHttpRequestType_stoneobs_enum)method
             parameters:(id)parameters
                success:(STNetSuccess_stoneobs_handle)success
                failure:(STNetFail_stoneobs_handle)failure
{
    parameters = [(NSDictionary*)parameters mutableCopy];
    
    [parameters setObject:@"ios" forKey:@"user_from"];
    NSString * version = [NSString stringWithFormat:@"apple_V%@",UIApplication.stoneobs_fun_st_applictionVersin];
    [parameters setObject:version forKey:@"version"];
    [parameters setObject:UIApplication.st_iphone_type forKey:@"model"];
    [parameters setObject:[NSString stringWithFormat:@"APPLE+%@",UIApplication.stoneobs_fun_st_uuid] forKey:@"IMEI"];
    [parameters setObject:@"iOS" forKey:@"useragent"];
    [parameters setObject:UIApplication.stoneobs_fun_st_applictionVersin forKey:@"version"];
    NSString * token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (token.length) {
       [_manager.requestSerializer setValue:token forHTTPHeaderField:@"Authorization"];
    }
    NSString * newUrl = url;
    if (method == STHttpRequestType_stoneobs_enumPost) {
        [_manager POST:newUrl parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
            if ([retCode isEqualToString:st_resp_tureCode]) {
                if (success) {
                    success(task,responseObject);
                }
            } else {
                if ([retCode isEqualToString:@"401"]) {
                    //登录过期
                    [TMUtils.stoneobs_fun_get_currentTopController stoneobs_fun_st_showAlertTitle:@"登录已失效请重新登陆" message:@"" leftTitle:@"" rightTitle:@"确认" block:^(NSString *name) {
                        UIWindow * window = UIApplication.sharedApplication.delegate.window;
                        [STTools_UserManger.defult stoneobs_fun_removeUserPreferce];
                        RTRootNavigationController * nav = [[RTRootNavigationController alloc] initWithRootViewController:UIViewController.new];
                        window.rootViewController = nav;
                        [window stoneobs_fun_st_showAnimationWithType:STAnimationTypecube_stoneobs_enum];
                        [[STTools_RcImManger manger] logout];
                    }];
                    //                    AUTO_DO_NO_LOGIN_CODE
                    return ;
                }
                if (failure) {

                    STTools_Error * serror = STTools_Error.new;
                    serror.desc = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_messageKey]];
                    serror.code = retCode.integerValue;
                    serror.resp = responseObject;
                    failure(retCode,serror,nil);
                }
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
    }else if (method == STHttpRequestType_stoneobs_enumGet) {
        [_manager GET:newUrl parameters:parameters headers:nil
             progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
             if ([retCode isEqualToString:st_resp_tureCode]) {
                 if (success) {
                     success(task,responseObject);
                 }
             } else {
                 if ([retCode isEqualToString:@"401"]) {
                     //登录过期
                     [TMUtils.stoneobs_fun_get_currentTopController stoneobs_fun_st_showAlertTitle:@"登录已失效请重新登陆" message:@"" leftTitle:@"" rightTitle:@"确认" block:^(NSString *name) {
                         UIWindow * window = UIApplication.sharedApplication.delegate.window;
                         [STTools_UserManger.defult stoneobs_fun_removeUserPreferce];
                         RTRootNavigationController * nav = [[RTRootNavigationController alloc] initWithRootViewController:UIViewController.new];
                         window.rootViewController = nav;
                         [window stoneobs_fun_st_showAnimationWithType:STAnimationTypecube_stoneobs_enum];
                         [[STTools_RcImManger manger] logout];
                     }];
                     return ;
                 }
                 
                 if (failure) {
                     STTools_Error * serror = STTools_Error.new;
                     serror.desc = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_messageKey]];
                     serror.code = retCode.integerValue;
                     serror.resp = responseObject;
                     failure(retCode,serror,nil);
                 }
                 
             }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
       
    }else if (method == STHttpRequestType_stoneobs_enumPut) {
        [_manager PUT:newUrl parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
            if ([retCode isEqualToString:st_resp_tureCode]) {
                if (success) {
                    success(task,responseObject);
                }
            } else {
                if (failure) {
                    STTools_Error * serror = STTools_Error.new;
                    serror.desc = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_messageKey]];
                    serror.code = retCode.integerValue;
                    failure(retCode,serror,nil);
                }
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
        
    }else if (method == STHttpRequestType_stoneobs_enumDelete) {
        [_manager DELETE:newUrl parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
            if ([retCode isEqualToString:st_resp_tureCode]) {
                if (success) {
                    success(task,responseObject);
                }
            } else {
                if (failure) {
                    STTools_Error * serror = STTools_Error.new;
                    serror.desc = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_messageKey]];
                    serror.code = retCode.integerValue;
                    failure(retCode,serror,nil);
                }
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
        
    }
}
- (void)stoneobs_fun_requestIgnoreSuccessWithPath:(NSString *)url method:(STHttpRequestType_stoneobs_enum)method parameters:(id)parameters success:(STNetSuccess_stoneobs_handle)success failure:(STNetFail_stoneobs_handle)failure{
    NSString * newUrl = url;
    [_manager.requestSerializer setValue:@"" forHTTPHeaderField:@"app-token"];
    if (method == STHttpRequestType_stoneobs_enumPost) {
        [_manager POST:newUrl parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success) {
                success(task,responseObject);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
    }else if (method == STHttpRequestType_stoneobs_enumGet) {
        [_manager GET:newUrl parameters:parameters headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success) {
                success(task,responseObject);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
    }
    
}
//有返回值的请求
- (NSURLSessionDataTask *)stoneobs_fun_taskstoneobs_fun_requestWithPath:(NSString *)url
                                       method:(STHttpRequestType_stoneobs_enum)method
                                   parameters:(id)parameters
                                      success:(STNetSuccess_stoneobs_handle)success
                                      failure:(STNetFail_stoneobs_handle)failure
{
    if (method == STHttpRequestType_stoneobs_enumPost) {
        return  [_manager POST:url parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
            if ([retCode isEqualToString:st_resp_tureCode]) {
                success(task,responseObject);
            }else {
                if (failure) {
                    STTools_Error * serror =  STTools_Error.new;
                    serror.desc = [responseObject valueForKey:st_resp_messageKey];
                    serror.code = retCode.integerValue;
                    failure(retCode,serror,nil);
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
    }else if (method == STHttpRequestType_stoneobs_enumGet) {
        return    [_manager GET:url parameters:parameters headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
            if ([retCode isEqualToString:st_resp_tureCode]) {
                success(task,responseObject);
            }else {
                if (failure) {
                    STTools_Error * serror =  STTools_Error.new;
                    serror.desc = [responseObject valueForKey:st_resp_messageKey];
                    serror.code = retCode.integerValue;
                    failure(retCode,serror,nil);
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = error.userInfo[@"NSDebugDescription"];
                serror.code =  error.code;
                failure(@(error.code).description,serror,error);
            }
        }];
    }
    return nil;
}
//下载文件
- (NSURLSessionDownloadTask *)stoneobs_fun_downLoadWithUrl:(NSString *)url
                                     progress:(STNetProgress_stoneobs_handle)progress
                                      success:(STNetFinsh_stoneobs_handle)CompletionHandler
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];
    NSURLSessionDownloadTask *download =  [_manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress);
        }
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *createDir = [NSString stringWithFormat:@"%@/%@/Files", st_down_flodleName,pathDocuments];
        
        // 判断文件夹是否存在，如果不存在，则创建
        if (![[NSFileManager defaultManager] fileExistsAtPath:createDir]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
        } else {
            NSLog(@"FileDir is exists.");
        }
        NSString *path = [createDir stringByAppendingPathComponent:response.suggestedFilename];
        return [NSURL fileURLWithPath:path];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        if (CompletionHandler) {
            CompletionHandler(response, filePath, error);
        }
        
    }];
    return download;
}
- (void)stoneobs_fun_imagerequestWithPath:(NSString *)url
                  parameters:(id)parameters
                       image:(UIImage*)image
                    progress:(STNetProgress_stoneobs_handle)progress
                     success:(STNetSuccess_stoneobs_handle)success
                     failure:(STNetFail_stoneobs_handle)failure
{
    [_manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        long long time = [[NSDate date] timeIntervalSince1970]* 1000;
        [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.8) name:@"file" fileName:[NSString stringWithFormat:@"%lld.png",time] mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
        if ([retCode isEqualToString:st_resp_tureCode]) {
            success(task,responseObject);
        }else {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = [responseObject valueForKey:st_resp_messageKey];
                serror.code = retCode.integerValue;
                failure(retCode,serror,nil);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            STTools_Error * serror =  STTools_Error.new;
            serror.desc = error.userInfo[@"NSDebugDescription"];
            serror.code =  error.code;
            failure(@(error.code).description,serror,error);
        }
    }];
    
}
//附带imageName 参数
- (void)stoneobs_fun_imagerequestWithPath:(NSString *)url
                  parameters:(id)parameters
                       image:(UIImage*)image
                   imageName:(NSString*)imageName
                    progress:(STNetProgress_stoneobs_handle)progress
                     success:(STNetSuccess_stoneobs_handle)success
                     failure:(STNetFail_stoneobs_handle)failure{
    
    [_manager.requestSerializer setValue:@"" forHTTPHeaderField:@"app-token"];
    
    [_manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        long long time = [[NSDate date] timeIntervalSince1970]* 1000;
        [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5) name:imageName fileName:[NSString stringWithFormat:@"%lld.png",time] mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
        if ([retCode isEqualToString:st_resp_tureCode]) {
            success(task,responseObject);
        }else {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = [responseObject valueForKey:st_resp_messageKey];
                serror.code = retCode.integerValue;
                failure(retCode,serror,nil);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            STTools_Error * serror =  STTools_Error.new;
            serror.desc = error.userInfo[@"NSDebugDescription"];
            serror.code =  error.code;
            failure(@(error.code).description,serror,error);
        }
    }];
    
}
- (void)stoneobs_fun_imagesrequestWithPath:(NSString *)url
                   parameters:(id)parameters
                       images:(NSArray*)imageArray
                     progress:(STNetProgress_stoneobs_handle)progress
                      success:(STNetSuccess_stoneobs_handle)success
                      failure:(STNetFail_stoneobs_handle)failure
{
    [_manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        long long time = [[NSDate date] timeIntervalSince1970]* 1000;
        for (NSInteger i=1; i<=imageArray.count;i++ ) {
            UIImage * ima =imageArray[i-1];
            
            NSData * data =UIImageJPEGRepresentation(ima, 0.5);
            [formData appendPartWithFileData:data name:@"images" fileName:[NSString stringWithFormat:@"%lld-%ld.png",time,i] mimeType:@"image/png"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
        if ([retCode isEqualToString:st_resp_tureCode]) {
            success(task,responseObject);
        }else {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = [responseObject valueForKey:st_resp_messageKey];
                serror.code = retCode.integerValue;
                failure(retCode,serror,nil);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            STTools_Error * serror =  STTools_Error.new;
            serror.desc = error.userInfo[@"NSDebugDescription"];
            serror.code =  error.code;
            failure(@(error.code).description,serror,error);
        }
    }];
}
//iamge参数
- (void)stoneobs_fun_imagesrequestWithPath:(NSString *)url
                   parameters:(id)parameters
                       images:(NSArray*)imageArray
                   imageNames:(NSArray*)imageNames
                     progress:(STNetProgress_stoneobs_handle)progress
                      success:(STNetSuccess_stoneobs_handle)success
                      failure:(STNetFail_stoneobs_handle)failure{
    [_manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        long long time = [[NSDate date] timeIntervalSince1970]* 1000;
        for (NSInteger i=1; i<=imageArray.count;i++ ) {
            UIImage * ima =imageArray[i-1];
            NSData * data =UIImageJPEGRepresentation(ima, 0.5);
            [formData appendPartWithFileData:data name:imageNames[i -  1] fileName:[NSString stringWithFormat:@"%lld-%ld.png",time,i] mimeType:@"image/png"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
        if ([retCode isEqualToString:st_resp_tureCode]) {
            success(task,responseObject);
        }else {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = [responseObject valueForKey:st_resp_messageKey];
                serror.code = retCode.integerValue;
                failure(retCode,serror,nil);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            STTools_Error * serror =  STTools_Error.new;
            serror.desc = error.userInfo[@"NSDebugDescription"];
            serror.code =  error.code;
            failure(@(error.code).description,serror,error);
        }
    }];
}
- (void)stoneobs_fun_uploadFileWithUrl:(NSString *)url
                 filePath:(NSString *)filePath
               parameters:(id)parameters
                 progress:(STNetProgress_stoneobs_handle)progress
                  success:(STNetSuccess_stoneobs_handle)success
                  failure:(STNetFail_stoneobs_handle)failure
{
    [_manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
        [formData appendPartWithFileURL:fileUrl name:@"file" error:nil];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
        if ([retCode isEqualToString:st_resp_tureCode]) {
            success(task,responseObject);
        }else {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = [responseObject valueForKey:st_resp_messageKey];
                serror.code = retCode.integerValue;
                failure(retCode,serror,nil);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            STTools_Error * serror =  STTools_Error.new;
            serror.desc = error.userInfo[@"NSDebugDescription"];
            serror.code =  error.code;
            failure(@(error.code).description,serror,error);
        }
    }];
}
- (NSURLSessionDataTask *)stoneobs_fun_sessionuploadFileWithUrl:(NSString *)url
                                          filePath:(NSString *)filePath
                                        parameters:(id)parameters
                                          progress:(STNetProgress_stoneobs_handle)progress
                                           success:(STNetSuccess_stoneobs_handle)success
                                           failure:(STNetFail_stoneobs_handle)failure{
    
    return     [_manager POST:url parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
        [formData appendPartWithFileURL:fileUrl name:@"file" error:nil];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *retCode = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:st_resp_codeKey]];
        if ([retCode isEqualToString:st_resp_tureCode]) {
            success(task,responseObject);
        }else {
            if (failure) {
                STTools_Error * serror =  STTools_Error.new;
                serror.desc = [responseObject valueForKey:st_resp_messageKey];
                serror.code = retCode.integerValue;
                failure(retCode,serror,nil);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            STTools_Error * serror =  STTools_Error.new;
            serror.desc = error.localizedDescription;
            serror.code =  error.code;
            failure(@(error.code).description,serror,error);
        }
    }];
}

@end
